package com.zzyl.common.core.domain;

import java.io.Serializable;
import com.zzyl.common.constant.HttpStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 响应信息主体
 *
 * @author ruoyi
 */
@Api(tags = "通用响应结果")
@ApiModel(description = "通用响应结果封装")
public class R<T> implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final int SUCCESS = HttpStatus.SUCCESS;

    /** 失败 */
    public static final int FAIL = HttpStatus.ERROR;

    @ApiModelProperty(value = "响应状态码", example = "200")
    private int code;

    @ApiModelProperty(value = "响应消息")
    private String msg;

    @ApiModelProperty(value = "响应数据")
    private T data;

    @ApiOperation("返回成功响应(无数据)")
    public static <T> R<T> ok()
    {
        return restResult(null, SUCCESS, "操作成功");
    }

    @ApiOperation("返回成功响应")
    public static <T> R<T> ok(@ApiParam(value = "响应数据") T data)
    {
        return restResult(data, SUCCESS, "操作成功");
    }

    @ApiOperation("返回成功响应")
    public static <T> R<T> ok(@ApiParam(value = "响应数据") T data,
                              @ApiParam(value = "响应消息") String msg)
    {
        return restResult(data, SUCCESS, msg);
    }

    @ApiOperation("返回失败响应(无数据)")
    public static <T> R<T> fail()
    {
        return restResult(null, FAIL, "操作失败");
    }

    @ApiOperation("返回失败响应")
    public static <T> R<T> fail(@ApiParam(value = "响应消息") String msg)
    {
        return restResult(null, FAIL, msg);
    }

    @ApiOperation("返回失败响应")
    public static <T> R<T> fail(@ApiParam(value = "响应数据") T data)
    {
        return restResult(data, FAIL, "操作失败");
    }

    @ApiOperation("返回失败响应")
    public static <T> R<T> fail(@ApiParam(value = "响应数据") T data,
                                @ApiParam(value = "响应消息") String msg)
    {
        return restResult(data, FAIL, msg);
    }

    @ApiOperation("返回失败响应")
    public static <T> R<T> fail(@ApiParam(value = "状态码", required = true) int code,
                                @ApiParam(value = "响应消息", required = true) String msg)
    {
        return restResult(null, code, msg);
    }

    private static <T> R<T> restResult(@ApiParam(value = "响应数据") T data,
                                       @ApiParam(value = "状态码", required = true) int code,
                                       @ApiParam(value = "响应消息", required = true) String msg)
    {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    @ApiModelProperty(value = "获取状态码")
    public int getCode()
    {
        return code;
    }

    @ApiModelProperty(value = "设置状态码")
    public void setCode(@ApiParam(value = "状态码", required = true) int code)
    {
        this.code = code;
    }

    @ApiModelProperty(value = "获取响应消息")
    public String getMsg()
    {
        return msg;
    }

    @ApiModelProperty(value = "设置响应消息")
    public void setMsg(@ApiParam(value = "响应消息", required = true) String msg)
    {
        this.msg = msg;
    }

    @ApiModelProperty(value = "获取响应数据")
    public T getData()
    {
        return data;
    }

    @ApiModelProperty(value = "设置响应数据")
    public void setData(@ApiParam(value = "响应数据") T data)
    {
        this.data = data;
    }

    @ApiOperation("判断响应是否为失败")
    public static <T> Boolean isError(@ApiParam(value = "响应结果", required = true) R<T> ret)
    {
        return !isSuccess(ret);
    }

    @ApiOperation("判断响应是否为成功")
    public static <T> Boolean isSuccess(@ApiParam(value = "响应结果", required = true) R<T> ret)
    {
        return R.SUCCESS == ret.getCode();
    }
}
